<?php

namespace App\Http\Controllers\Admin;

use App\Models\Classify;
use App\Models\Goods;
use App\Models\GoodsImage;
use App\Models\HotGoods;
use App\Models\Pictures;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

class GoodsController extends Controller
{
    public function lists(Request $request)
    {
        $admin =Session::get('admin');
        if($admin->type==1){
            if($request->has('keywords')){
                $goods = Goods::where('name','like',"%".$request->get('keywords')."%")->paginate(30);

            }else{
                $goods = Goods::orderBy('status','desc')->orderBy('created_at')->paginate(20);
            }

            foreach ($goods as $value){
                $img = $value->my_picture()->where('type',1)->first();
                if($img==null){
                    $value->my_cover="";
                }else{
                    $cover = Pictures::find($img->pic_id)->toArray();
                    $value->my_cover = $cover['pic_path'];
                }
            };
        }else{
            if($request->has('keywords')){
                $goods = Goods::where('name','like',"%".$request->get('keywords')."%")
                    ->where('create_by',$admin->admin_id)
                    ->paginate(100);
            }else{
                $goods = Goods::paginate(20);
            }

            foreach ($goods as $value){
                $img = $value->my_picture()->where('type',1)->first();
                if($img==null){
                    $value->my_cover="";
                }else{
                    $cover = Pictures::find($img->pic_id)->toArray();
                    $value->my_cover = $cover['pic_path'];
                }
            };
        }

        return view('admin.goods_list',['goods'=>$goods]);
    }

    public function up_down(Request $request)
    {
        Goods::where('goods_id',$request->get('goods_id'))->update(['status'=>$request->get('type')]);
        return redirect()->back()->with(['message'=>'修改成功']);
    }

    public function create(Request $request)
    {
        $classify = Classify::where('level',1)->get();

        return view('admin.goods_create',[
            'classify'=>$classify
        ]);
    }

    public function store(Request $request)
    {
        $admin =Session::get('admin');
        $data=[
            'name'=>$request->get('name'),
            'stock'=>$request->get('stock'),
            'price'=>$request->get('price'),
            'class_id'=>$request->get('classify'),
            'goods_sn'=>'G'.time(),
            'create_by'=>$admin->admin_id,
            'detail'=>$request->get('detail'),
        ];
        if($admin->type==2){
            $data['status']=4;
        }else{
            $data['status']=$request->get('status');
        }
        $goods = Goods::create($data);

        $cover = $this->upload_img($request->cover,'upload/goods_cover',3);
        $c_img=[
            'type'=>1,
            'pic_id'=>$cover['img']['pic_id'],
            'goods_id'=>$goods->goods_id,
        ];
        GoodsImage::create($c_img);
        foreach ($request->banner as $banner){
            $banner = $this->upload_img($banner,'upload/goods_banner',4);
            $img=[
                'type'=>2,
                'pic_id'=>$banner['img']['pic_id'],
                'goods_id'=>$goods->goods_id,
            ];
            GoodsImage::create($img);
        }
        return back()->with(['message'=>"添加成功"]);
    }

    public function edit($id)
    {
        $goods = Goods::find($id);

        if($goods==null ){
            return back()->with(['message'=>"商品数据不全"]);
        }
        $all_classify = Classify::where('level',1)->get();
        $classify = Classify::find($goods->class_id);
        if($classify==null){
            return back()->with(['message'=>"商品数据不全"]);
        }
        $pa_classify = Classify::find($classify->pa_id);

        if($pa_classify==null){
            return back()->with(['message'=>"商品数据不全"]);
        }
        $son_classify = Classify::where('pa_id',$pa_classify->classify_id)->get();




        return view('admin.goods_edit',[
            'goods'=>$goods,'classify'=>$classify,'pa_classify'=>$pa_classify,
            'all_classify'=>$all_classify,'son_classify'=>$son_classify,'goods_id'=>$id
        ]);
    }


    public function update(Request $request)
    {
        $goods = Goods::find($request->get('goods_id'));
        $goods->name = $request->get('name');
        $goods->price = $request->get('price');
        $goods->stock = $request->get('stock');
        $goods->class_id = $request->get('classify');
        $goods->detail = $request->get('detail');
        $goods->status = $request->get('status');
        $goods->save();
        if($request->hasFile('cover')){
            GoodsImage::where([['goods_id',$request->get('goods_id')],['type',1]])->delete();
            $cover = $this->upload_img($request->cover,'upload/goods_cover',3);
            $c_img=[
                'type'=>1,
                'pic_id'=>$cover['img']['pic_id'],
                'goods_id'=>$goods->goods_id,
            ];
            GoodsImage::create($c_img);
        }

        if($request->hasFile('banner')){
            GoodsImage::where([['goods_id',$request->get('goods_id')],['type',2]])->delete();
            foreach ($request->banner as $banner){
                $banner = $this->upload_img($banner,'upload/goods_banner',4);
                $img=[
                    'type'=>2,
                    'pic_id'=>$banner['img']['pic_id'],
                    'goods_id'=>$goods->goods_id,
                ];
                GoodsImage::create($img);
            }
        }
        return redirect(url('goods/lists'))->with(['message'=>"修改成功"]);
    }


    public function hot(Request $request)
    {
        if($request->isMethod('post')){
            if($request->has('del')){
                HotGoods::destroy($request->get('del'));
                return redirect()->back()->with(['message'=>"已删除"]);
            }
            if($request->has('ins')){
                $goods = Goods::where('goods_sn',$request->get('ins'))->first();
                if($goods==null){
                    return redirect()->back()->with(['message'=>"请填写正确的商品编号"]);
                }
                $hot = new HotGoods;
                $hot->goods_id=$goods->goods_id;
                $hot->type=1;
                $hot->save();
                return redirect()->back()->with(['message'=>"已添加"]);
            }

        }
        $hot_goods = HotGoods::where('type',1)->get();
        $arr=[];
        foreach ($hot_goods as $value){
            $goods = $value->goods()->first();
            $goods->hot_goods_id=$value->hot_goods_id;
            $goods->cover = $goods->my_picture()->where('type',1)->first()->my_pic_path;
            $arr[]=$goods;
        }
        return view('admin.hot',['goods'=>$arr]);
    }

    public function like(Request $request)
    {
        if($request->isMethod('post')){
            if($request->has('del')){
                HotGoods::destroy($request->get('del'));
                return redirect()->back()->with(['message'=>"已删除"]);
            }
            if($request->has('ins')){
                $goods = Goods::where('goods_sn',$request->get('ins'))->first();
                if($goods==null){
                    return redirect()->back()->with(['message'=>"请填写正确的商品编号"]);
                }
                $hot = new HotGoods;
                $hot->goods_id=$goods->goods_id;
                $hot->type=2;
                $hot->save();
                return redirect()->back()->with(['message'=>"已添加"]);
            }

        }
        $hot_goods = HotGoods::where('type',2)->get();
        $arr=[];
        foreach ($hot_goods as $value){
            $goods = $value->goods()->first();
            $goods->hot_goods_id=$value->hot_goods_id;
            $goods->cover = $goods->my_picture()->where('type',1)->first()->my_pic_path;
            $arr[]=$goods;
        }
        return view('admin.like',['goods'=>$arr]);
    }
}
